#!/bin/sh
# PCP QA Test No. 1067
# Exercise the zbxpcp Zabbix integration module.
#
# Copyright (c) 2015 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

which zabbix_agentd >/dev/null 2>&1 || \
	_notrun "No zabbix_agentd binary found"
test -f /etc/zabbix/zabbix_agentd.conf || \
	_notrun "No configuration for the Zabbix agent daemon"
test -f /etc/zabbix/zabbix_agentd.d/zbxpcp.conf || \
	_notrun "Zabbix agent PCP module is not installed"

rm -f $seq.out
# Known versions ...
# Zabbix Agent (daemon) v2.2.7 (revision 50148) (24 October 2014)
#	- no [error message] after [.|ZBX_NOTSUPPORTED]
# zabbix_agentd (daemon) (Zabbix) 3.0.7
#	- [error message] after [.|ZBX_NOTSUPPORTED]
#
version=`zabbix_agentd -V | sed -n -e '/^[Zz]abbix/{
s/ v/ /
s/^[^0-9]* \([0-9]\)/\1/
s/ .*//
p
}'`

case "$version"
in
    1.*)
		_notrun "zabbix_agentd $version too old (no LoadModulePath config option)"
		;;
    2.[0-2].*)
    		ln $seq.out.0 $seq.out
		;;
    2.4.*|3.*|4.*|5.*)
    		ln $seq.out.1 $seq.out
    		;;
    *)		echo "Botch: need to know what output to expect for zabbix_agent version $version"
    		exit
		;;
esac

case "$PCP_PLATFORM"
in
    darwin)
       export ZBXPCP=zbxpcp.dylib
       ;;
    mingw)
       export ZBXPCP=zbxpcp.dll
       ;;
    *)
       export ZBXPCP=zbxpcp.so
       ;;
esac

status=1	# failure is the default!
need_control=false
need_derived=false
control=$PCP_PMDAS_DIR/sample/dynamic.indom
derived=/etc/zabbix/zbxpcp-derived-metrics.conf
$sudo rm -rf $tmp $tmp.* $seq.full

echo "zabbix_agentd version: $version" >>$here/$seq.full

_cleanup()
{
    $need_control && _restore_config $control
    $need_derived && _restore_config $derived
    rm -f $tmp.*
}
trap "_cleanup; exit \$status" 0 1 2 3 15
if [ -f $control ]
then
    need_control=true
    _save_config $control
fi
if [ -f $derived ]
then
    need_derived=true
    _save_config $derived
fi

# pcp.sample.float.hundred                      [d|100.000000]
# pcp.sample.double.ten                         [d|10.000000]
#                 the decimal part may not be reported^^^^^^
#
_filter_float()
{
    sed \
	-e '/pcp.sample.float.hundred/s/100\.00*/100/' \
	-e '/pcp.sample.double.ten/s/10\.00*/10/' \
    # end
}

_filter_values()
{
    # expect lines like:
# pcp.sample.dodgey.control    [u|5]
# pcp.sample.dodgey.value[d1]  [u|35]
# pcp.sample.bad.novalues      [m|ZBX_NOTSUPPORTED] [No value available.]
    # d|u|s -> zabbix types;
    # m is error encodings -> allow through

    sed \
	-e 's/\[[dus]|.*\]$/VALUE/g' \
	-e '/sample\.scramble/d' \
    #end
}

cat <<EOF > $tmp.conf
LoadModulePath=$PCP_LIB_DIR/zabbix/modules
LoadModule=$ZBXPCP
# for rpm -qa if repo cache needs refreshing
Timeout=30
EOF

echo "zabbix_agentd config:" >>$seq.full
cat $tmp.conf >>$seq.full

# real QA test starts here
zabbix_agentd="zabbix_agentd -c $tmp.conf"
echo "== request specific values"

# verify values for single metrics/instances
$zabbix_agentd -t pcp.sample.long.one
$zabbix_agentd -t 'pcp.sample.long.bin[bin-100]'
$zabbix_agentd -t pcp.sample.ulonglong.million
$zabbix_agentd -t pcp.sample.float.hundred | _filter_float
$zabbix_agentd -t pcp.sample.double.ten | _filter_float

# string metrics
$zabbix_agentd -t pcp.sample.string.null
$zabbix_agentd -t pcp.sample.string.hullo

# aggregate/event metrics
$zabbix_agentd -t pcp.sample.aggregate.hullo
$zabbix_agentd -t 'pcp.sample.event.records[fungus]'

# other error cases
$zabbix_agentd -t pcp.sample.bad.nosupport
$zabbix_agentd -t pcp.sample.bad.novalues

# verify enumeration of all metrics
echo "== enumerated names and filtered values" | tee -a $seq.full
$sudo rm -f $control	# control the dynamic indom
echo '1 one' >$tmp.indom
echo '2 two' >>$tmp.indom
$sudo cp $tmp.indom $control
$zabbix_agentd -p \
| tee -a $seq.full \
| fgrep pcp.sample. \
| _filter_values

# derived metrics
echo "== check derived metric configuration"
echo mem.util.allcache = mem.util.cached + mem.util.slab | tee $tmp.derived
echo sample.derived.hundredten = sample.float.hundred + sample.double.ten | tee -a $tmp.derived
$sudo cp $tmp.derived $derived
$zabbix_agentd -t pcp.mem.util.allcache | _filter_values
$zabbix_agentd -t pcp.sample.derived.hundredten | _filter_values

$sudo rm -f $control $derived

# success, all done
status=0
exit
